//
//  SQPlaceBidView.m
//  pai
//
//  Created by pro on 14-11-17.
//  Copyright (c) 2014年 linjunjing. All rights reserved.
//

#import "SQPlaceBidView.h"

@implementation SQPlaceBidView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
-(id)initWithstartPrice:(NSString*)start endPrice:(NSString*)end
{
    self = [super initWithFrame:CGRectZero];
    if (self) {
        NSArray *array = [[NSBundle mainBundle]loadNibNamed:@"SQPlaceBidView" owner:self options:nil];
        self = [array objectAtIndex:0];
        NSMutableAttributedString* price = [[NSMutableAttributedString alloc]initWithString:[[NSString alloc] initWithFormat:@"%@~%@豆",start,end]];
        [price addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,start.length)];
        [price addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(start.length,1)];
        [price addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(start.length+1,end.length)];
        [price addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(price.length-1,1)];
        self.priceLabel.attributedText = price;
        
        [self.placeBidLabel addTarget:self action:@selector(moveDownTextField:) forControlEvents:UIControlEventEditingDidEndOnExit];
        
        self.placeBidLabel.delegate = self;
    }
    return self;
}



-(void)moveDownTextField:(UITextField* )text
{
    if (self.tempy) {
         self.frame = CGRectMake(self.tempx, self.tempy, 280, 240);
    }
    [text resignFirstResponder];
}

#pragma mark - UITextField Delegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    self.tempx = self.frame.origin.x;
    self.tempy = self.frame.origin.y;
    self.frame = CGRectMake(self.tempx, self.tempy-100, 280, 240);
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    [super touchesBegan:touches withEvent:event];
    if (self.tempy) {
        self.frame = CGRectMake(self.tempx, self.tempy, 280, 240);
    }
}
- (IBAction)doActionSubmit:(id)sender {
    NSLog(@"submit");
    if (self.delegate) {
        NSLog(@"submit");
        [self.delegate PlaceBidSumbmit:self.placeBidLabel.text];
    }
    [self moveDownTextField:self.placeBidLabel ];
}

- (IBAction)doActionBack:(id)sender {
    NSLog(@"doActionCancle");
    if (self.delegate) {
        NSLog(@"doActionCancle");
        [self.delegate PlaceBidCancle];
    }
    [self moveDownTextField:self.placeBidLabel ];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
